<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增课件')"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-courseware-add">
        <input type="hidden" name="chapterId" th:value="${chapterId}">
        <div class="form-group">
            <label class="col-sm-3 control-label">课件名称：</label>
            <div class="col-sm-8">
                <input name="coursewareName" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <textarea name="remark" class="form-control"></textarea>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">文件：</label>
            <div class="col-sm-8">
                <input type="hidden" name="coursewareUrl">
                <div class="file-loading">
                    <input class="form-control file-upload" id="coursewareUrl" name="file" type="file">
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<script th:inline="javascript">
    var prefix = ctx + "courseware/courseware"
    $("#form-courseware-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            add()
        }
    }

    function add() {
        var formData = new FormData();
        formData.append("chapterId", $("[name=chapterId]").val())
        formData.append("coursewareName", $("[name=coursewareName]").val())
        formData.append("remark", $("[name=remark]").val())
        if ($('#coursewareUrl')[0].files[0] != undefined) {
            formData.append('coursewareFile', $('#coursewareUrl')[0].files[0]);
        }
        $.ajax({
            url: prefix + "/add",
            type: 'post',
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
            dataType: "json",

            success: function (result) {
                $.operate.successCallback(result);
            }
        });
    }

    $(".file-upload").fileinput({
        maxFileCount: 1,
        autoReplace: true,
        showUpload: false, //是否显示上传按钮
        showPreview: false,//是否预览
        dropZoneTitle: "拖拽文件到这里 …" + `<br>` + "仅支持单文件上传",
    }).on('fileuploaded', function (event, data, previewId, index) {
        $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
    }).on('fileremoved', function (event, id, index) {
        $("input[name='" + event.currentTarget.id + "']").val('')
    })
</script>
</body>
</html>